using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace GPUAPP2
{
    public class DataGenerator
    {
        private readonly int seedStart;
        private readonly int seedEnd;
        private readonly Random random;
        private int[] currentPermutation;
        private bool[] used;
        private int currentPosition;
        private bool isFirstCall = true;

        public DataGenerator(int seedStart, int seedEnd)
        {
            this.seedStart = seedStart;
            this.seedEnd = seedEnd;
            this.random = new Random();
            InitializePermutation();
        }

        private void InitializePermutation()
        {
            currentPermutation = new int[30];
            used = new bool[30];
            currentPosition = 0;
            for (int i = 0; i < 30; i++)
            {
                currentPermutation[i] = i;
            }
        }

        public int[][] GeneratePermutationData(int count)
        {
            if (count <= 0)
                return new int[0][];

            List<int[]> result = new List<int[]>();
            
            for (int i = 0; i < count; i++)
            {
                if (!GenerateNextPermutation())
                {
                    // 如果已经生成了所有可能的排列，重新开始
                    InitializePermutation();
                    if (!GenerateNextPermutation())
                    {
                        break;
                    }
                }
                
                // 复制当前排列到结果中
                int[] permutation = new int[30];
                Array.Copy(currentPermutation, permutation, 30);
                result.Add(permutation);
            }

            return result.ToArray();
        }

        private bool GenerateNextPermutation()
        {
            if (isFirstCall)
            {
                isFirstCall = false;
                return true;
            }

            // 从后向前找到第一个可以交换的位置
            int i = 29;
            while (i > 0 && currentPermutation[i - 1] >= currentPermutation[i])
            {
                i--;
            }

            if (i <= 0)
            {
                return false; // 已经是最后一个排列
            }

            // 找到可以交换的最小值
            int j = 29;
            while (currentPermutation[j] <= currentPermutation[i - 1])
            {
                j--;
            }

            // 交换
            Swap(ref currentPermutation[i - 1], ref currentPermutation[j]);

            // 反转后面的部分
            j = 29;
            while (i < j)
            {
                Swap(ref currentPermutation[i], ref currentPermutation[j]);
                i++;
                j--;
            }

            return true;
        }

        private void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        public int[][] GenerateRandomData(int rowCount)
        {
            int[][] result = new int[rowCount][];
            int range = seedEnd - seedStart + 1;
            int[] numbers = Enumerable.Range(seedStart, range).ToArray();

            for (int i = 0; i < rowCount; i++)
            {
                // 创建0-29的数组并随机打乱
                int[] row = new int[range];
                Array.Copy(numbers, row, range);
                ShuffleArray(row);
                result[i] = row;
            }

            return result;
        }

        private void ShuffleArray(int[] array)
        {
            for (int i = array.Length - 1; i > 0; i--)
            {
                int j = random.Next(i + 1);
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        private BigInteger Factorial(int n)
        {
            if (n <= 1) return 1;
            BigInteger result = 1;
            for (int i = 2; i <= n; i++)
            {
                result *= i;
            }
            return result;
        }
    }
} 